package info.rongtong.service.impl;

import info.rongtong.domain.Article;
import info.rongtong.domain.IndexArticle;
import info.rongtong.service.inter.ILucene;
import info.rongtong.utils.LuceneUtil;

import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MultiPhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.util.Version;
import org.aspectj.lang.JoinPoint;

public class LuceneServiceImpl implements ILucene {

	private final int pageSize = 2; 
	
	public void saveLucene(JoinPoint article) throws Exception{
		System.out.println("=======save===============");
		Document doc = LuceneUtil.ArticleToDocument((Article) article.getArgs()[0]);
		LuceneUtil.getIndexWriter().addDocument(doc);
		LuceneUtil.getIndexWriter().commit();
	}
	
	public void updateLucene(JoinPoint goods) throws Exception{
		System.out.println("=======update===============");
		Document doc = LuceneUtil.ArticleToDocument(((Article)goods.getArgs()[0]));
		LuceneUtil.getIndexWriter().updateDocument(new Term("gid", doc.get("gid")), doc);
		LuceneUtil.getIndexWriter().commit();
	}
	
	public void deleteLucene(JoinPoint goods) throws Exception{
		System.out.println("=======del===============");
		LuceneUtil.getIndexWriter().deleteDocuments(new Term("gid",goods.getArgs()[0].toString()));
		LuceneUtil.getIndexWriter().commit();
	}
	
	public List<Article> queryIndex() throws Exception{
		List<Article> list = new ArrayList<Article>();
		DirectoryReader indexReader =DirectoryReader.open(LuceneUtil.getIndexWriter(), false);
		for(int i=0;i<indexReader.maxDoc();i++){
			list.add(LuceneUtil.DocumentToArticle((indexReader.document(i))));
		}
		return list;
	}
	
	public IndexArticle  queryIndex(String key,int currentPage) throws Exception{
		IndexArticle indexGoods = new IndexArticle();
		List<Article> list = new ArrayList<Article>();
		
		MultiPhraseQuery queryParser = new MultiPhraseQuery();
		//(Version.LUCENE_42, new String[]{"gname","gremark"}, LuceneUtil.getIkAnalyzer());
		Query query = queryParser.parse(key);
//		Query query = new TermQuery(new Term(type, key));
		IndexReader indexReader =IndexReader.open(LuceneUtil.getDirectory());
		IndexSearcher indexSercher = new IndexSearcher(indexReader);
		Sort sort = new Sort(new SortField("gid", SortField.INT,true));
		TopDocs topDocs = indexSercher.search(query, pageSize*currentPage,sort);
		int endIndex = Math.min(topDocs.scoreDocs.length,pageSize*currentPage);
		for(int i=pageSize*(currentPage-1);i<endIndex;i++){
			Sgoods goods = LuceneUtil.documentToSgoods(indexReader.document(topDocs.scoreDocs[i].doc));
			goods.setSgname(LuceneUtil.highlighterGremark(query, 15, goods.getSgname()));
			goods.setSgremark(LuceneUtil.highlighterGremark(query, 30, goods.getSgremark()));
			list.add(goods);
		}
		indexGoods.setGoodsList(list);
		indexGoods.setPageCount((topDocs.totalHits-1)/pageSize+1);
		return indexGoods;
	}
	
	
}
